package com.conf;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.ServletRequestHandledEvent;


@Component
public class ServletRequestHandledEventListener implements ApplicationListener<ServletRequestHandledEvent> {

    private final static Logger logger = LoggerFactory.getLogger(ServletRequestHandledEventListener.class);

    @Override
    public void onApplicationEvent(ServletRequestHandledEvent event) {
        // 返回请求的URL。
        String url = event.getRequestUrl();
        // 返回请求来源的IP地址。
        String client = event.getClientAddress();
        // 以毫秒为单位返回请求的处理时间。
        long time = event.getProcessingTimeMillis();
        // 返回请求的HTTP方法(通常是GET或POST)。
        String method = event.getMethod();
        // 返回响应的HTTP状态码
        int statusCode = event.getStatusCode();
        logger.info("客户端IP: {}, 请求的URL: {}, HTTP方法: {}, HTTP状态码: {}, 请求处理时长: {}ms", client, url, method, statusCode, time);
    }

}